"""
统一响应格式模块
"""

from flask import jsonify
from typing import Any, Dict, Optional


def success_response(data: Any = None, msg: str = "success", code: int = 200) -> Dict:
    """
    成功响应
    
    Args:
        data: 响应数据
        msg: 响应消息
        code: 响应码
        
    Returns:
        响应字典
    """
    return jsonify({
        "code": code,
        "msg": msg,
        "data": data
    }), code


def error_response(msg: str = "error", code: int = 400, data: Any = None) -> Dict:
    """
    错误响应
    
    Args:
        msg: 错误消息
        code: 错误码
        data: 额外数据
        
    Returns:
        响应字典
    """
    return jsonify({
        "code": code,
        "msg": msg,
        "data": data
    }), code


def paginate_response(items: list, page: int, page_size: int, total: int) -> Dict:
    """
    分页响应
    
    Args:
        items: 数据列表
        page: 当前页码
        page_size: 每页数量
        total: 总数量
        
    Returns:
        分页响应字典
    """
    return success_response({
        "items": items,
        "page": page,
        "page_size": page_size,
        "total": total,
        "total_pages": (total + page_size - 1) // page_size
    })
